from pkgr.tests.pkgrtestcase import PkgrTestCase
import os
import logging
import solv
import rpm
def pp_jobs(jobs):
    for job in jobs:
        for s in job.solvables():
            #if cmd == 'info':
                print "Name:        %s" % s
                print "Repo:        %s" % s.repo
                print "Summary:     %s" % s.lookup_str(solv.SOLVABLE_SUMMARY)
                str = s.lookup_str(solv.SOLVABLE_URL)
                if str:
                    print "Url:         %s" % str
                str = s.lookup_str(solv.SOLVABLE_LICENSE)
                if str:
                    print "License:     %s" % str
                print "Description:\n%s" % s.lookup_str(solv.SOLVABLE_DESCRIPTION)
                print
            #else:
                print "  - %s [%s]" % (s, s.repo)
                print "    %s" % s.lookup_str(solv.SOLVABLE_SUMMARY)
logger = logging.getLogger('pkgr.test.test_pkgr')
class Test_Pkg_Basic(PkgrTestCase):
        
    def test_erase(self):
        """ Test erasing a package. """
        pkgr = self.pkgr
        
        pool = pkgr.get_solv_pool()
        jobs = pkgr.names_to_jobs('install', ['a'])
        pkgr.set_jobs(jobs)
        
        for job in jobs:
            job.how |= solv.Job.SOLVER_INSTALL
            
        solver = pool.Solver()
        solver.set_flag(solv.Solver.SOLVER_FLAG_SPLITPROVIDES, 1);
        
        problems = solver.solve(jobs)
        
        self.assertTrue(len(problems) == 0, "Solving should not return problems")
        
        # no problems, show transaction
        trans = solver.transaction()
        # gc/clear refcounts to the solver
        del solver
        
        self.assertTrue(not trans.isempty())
        
        rpmrunproblems = pkgr.run_transaction(trans)
        
        self.assertTrue(not rpmrunproblems, "Running rpm transaction shouldn't have problems.")
        
        if rpmrunproblems:
            print rpmrunproblems
            
        self.assertTrue(os.path.exists(os.path.join(self.installroot, 'a')))
        self.dump_install_root()
        
        
        ts = rpm.TransactionSet(self.installroot)
        pkgs = ts.dbMatch() # RPM package list
        pkgs.pattern( 'name', rpm.RPMMIRE_DEFAULT, 'a' )
        self.assertTrue(len(list(pkgs)) >= 1)
        
        
        
        
        ######################
        
        self.load_new_pkgr()
        pkgr = self.pkgr
        
        pool = pkgr.get_solv_pool()
        jobs = pkgr.names_to_jobs('erase', ['a'])
        

        print '!!!!!!!', jobs#, jobs[0].solvables()
        
    
            
        #for job in jobs:            
        #    print 'Loop Job-----'
        #    print job.pool
        #    print job.how
        #    print job.solvables()
        
        for job in jobs:
            job.how |= solv.Job.SOLVER_ERASE
            print 'Loop 2 Job-----'
            logging.debug(job.pool)
            logging.debug(job.how)
            logging.debug(job.solvables())
            
        print 'Processed', jobs 
                
        pkgr.set_jobs(jobs)  
        pp_jobs(jobs)
        solver = pool.Solver()
        solver.set_flag(solv.Solver.SOLVER_FLAG_SPLITPROVIDES, 1);
        solver.set_flag(solv.Solver.SOLVER_FLAG_ALLOW_UNINSTALL, 1);
        
        problems = solver.solve(jobs)
        
        self.assertTrue(len(problems) == 0, "Solving should not return problems")
        
        # no problems, show transaction
        trans = solver.transaction()
        # gc/clear refcounts to the solver
        #del solver
        print "TRANS", trans, trans.steps()
        self.assertTrue(not trans.isempty())
        
        rpmrunproblems = pkgr.run_transaction(trans)
        
        self.assertTrue(not rpmrunproblems, "Running rpm transaction shouldn't have problems.")
        
        if rpmrunproblems:
            print rpmrunproblems
            
        self.assertTrue(not os.path.exists(os.path.join(self.installroot, 'a')))
        self.dump_install_root()
        
     
